import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Inject,
  Param,
  Post,
  Put,
  Query,
  Res,
  SetMetadata,
  // UseFilters,
  // UseGuards,
  // UseInterceptors,
  // UsePipes,
  // ValidationPipe,
} from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { CoffeesService } from './coffees.service';
import { CreateCoffeeDto } from './dto/create-coffee.dto';
import { UpdateCoffeeDto } from './dto/update-coffee.dto';
import { Request } from 'express';
import { Public } from 'src/common/decorator/public.decorator';
import { ParseIntPipe } from 'src/common/pipes/parse-int.pipe';

// @UsePipes(ValidationPipe)
// @UseFilters()
// @UseGuards()
// @UseInterceptors()
@Controller('coffees')
export class CoffeesController {
  // coffeesService 一个单例的实例
  constructor(
    private readonly coffeesService: CoffeesService,
    @Inject(REQUEST) private readonly request: Request,
  ) {
    console.log('CoffeeController created');
    // console.log(request);
  }

  // @UsePipes(ValidationPipe)
  // @SetMetadata('isPublic', true)
  @Public()
  @Get()
  findAll(@Query() paginationQuery: PaginationQueryDto) {
    return this.coffeesService.findAll(paginationQuery);
  }

  @Public()
  @Get('timeout')
  findAllTimeout(@Query() paginationQuery: PaginationQueryDto) {
    return this.coffeesService.findAll(paginationQuery, true);
  }

  // 不标准的状态
  @Get('all')
  findWithRes(@Res() response) {
    response
      .status(200)
      .send('this action returns all coffees at /coffees/all');
  }

  // 分页
  @Get('all2')
  findAll2(@Query() paginationQuery) {
    const { limit, offset } = paginationQuery;
    return `this action returns limit: ${limit} offset: ${offset} coffees`;
  }

  // 动态id
  @Get(':id')
  findById(@Param('id', ParseIntPipe) id: number) {
    console.log(id);
    return this.coffeesService.findOne(id);
  }

  // 自定义路由
  @Get('flavors')
  findAllFlavors() {
    return 'this action returns all coffees flavors';
  }

  // body
  @Post()
  create(@Body() createCoffeeDto: CreateCoffeeDto) {
    console.log(createCoffeeDto instanceof CreateCoffeeDto); // false
    return this.coffeesService.create(createCoffeeDto);
  }

  // 自定义body
  @Post('create1')
  create1_1(@Body('name') name) {
    return name;
  }

  // 自定义code
  @Post('create2')
  @HttpCode(HttpStatus.GONE)
  create2(@Body('name') name, @Body() body) {
    console.log(name, body);
    return name;
  }

  @Put(':id')
  update(
    @Param('id') id: number,
    // @Body(ValidationPipe) updateCoffeeDto: UpdateCoffeeDto,
    @Body() updateCoffeeDto: UpdateCoffeeDto,
  ) {
    return this.coffeesService.update(id, updateCoffeeDto);
  }

  @Delete(':id')
  del(@Param('id') id: number) {
    return this.coffeesService.remove(id);
  }
}
